# With Ubuntu18.04 googletest is updated to version 1.8.0 which changes the install directory structure. 
# Fix bug catkin build 
# CMake Error at /opt/ros/melodic/share/catkin/cmake/test/gtest.cmake:387 (add_subdirectory):
#  add_subdirectory given source "/usr/src/gmock" which is not an existing
#  directory.


find_package(GMock QUIET)
if(NOT GMOCK_FOUND)
  find_package(GTest QUIET)
  if(NOT GTEST_FOUND)
    # only add gmock/gtest directory once per workspace
    if(NOT TARGET gtest AND NOT TARGET gmock)
      # Fall back to system-installed gmock source (e.g. Ubuntu)
      set(_include_paths "/usr/include")
      set(_source_paths "/usr/src")

      catkin_find_google_test_source("${_include_paths}" "${_source_paths}" gtest_found
                                     gtest_include_dir gtest_lib_dir gtest_libs gtest_main_libs
                                     gmock_found gmock_include_dir gmock_lib_dir gmock_libs
                                     gmock_main_libs base_dir)
      if(gtest_found)
        set(GTEST_FROM_SOURCE_FOUND ${gtest_found} CACHE INTERNAL "")
        set(GTEST_FROM_SOURCE_INCLUDE_DIRS ${gtest_include_dir} CACHE INTERNAL "")
        set(GTEST_FROM_SOURCE_LIBRARY_DIRS ${gtest_lib_dir} CACHE INTERNAL "")
        set(GTEST_FROM_SOURCE_LIBRARIES ${gtest_libs} CACHE INTERNAL "")
        set(GTEST_FROM_SOURCE_MAIN_LIBRARIES ${gtest_main_libs} CACHE INTERNAL "")
        if(gmock_found)
          set(GMOCK_FROM_SOURCE_FOUND ${gmock_found} CACHE INTERNAL "")
          set(GMOCK_FROM_SOURCE_INCLUDE_DIRS ${gmock_include_dir} CACHE INTERNAL "")
          set(GMOCK_FROM_SOURCE_LIBRARY_DIRS ${gmock_lib_dir} CACHE INTERNAL "")
          set(GMOCK_FROM_SOURCE_LIBRARIES ${gmock_libs} CACHE INTERNAL "")
          set(GMOCK_FROM_SOURCE_MAIN_LIBRARIES ${gmock_main_libs} CACHE INTERNAL "")
          message(STATUS "Found gmock sources under '${base_dir}': gmock will be built")
        endif()
        if(base_dir)
          # overwrite CMake install command to skip install rules for gtest targets
          # which have been added in version 1.8.0
          _use_custom_install()
          set(_CATKIN_SKIP_INSTALL_RULES TRUE)
          add_subdirectory(${base_dir} ${gtest_lib_dir})
          set(_CATKIN_SKIP_INSTALL_RULES FALSE)
          set_target_properties(${gtest_libs} ${gtest_main_libs}
                                PROPERTIES EXCLUDE_FROM_ALL 1)
          if(gmock_found)
            set_target_properties(${gmock_libs} ${gmock_main_libs}
                                  PROPERTIES EXCLUDE_FROM_ALL 1)
          endif()
        endif()
        message(STATUS "Found gtest sources under '${base_dir}': gtests will be built")
      else()
        if(CATKIN_TOPLEVEL)
          message(STATUS "gtest not found, C++ tests can not be built. Please install the gtest headers globally in your system or checkout gtest (by running 'git clone  https://github.com/google/googletest.git -b release-1.8.0' in the source space '${CMAKE_SOURCE_DIR}' of your workspace) to enable gtests")
        else()
          message(STATUS "gtest not found, C++ tests can not be built. Please install the gtest headers globally in your system to enable gtests")
        endif()
      endif()
    endif()

    if(GMOCK_FROM_SOURCE_FOUND)
      # set the same variables as find_package()
      # do NOT set in the cache since when using gmock/gtest from source
      # we must always add the subdirectory to have their targets defined
      set(GMOCK_FOUND ${GMOCK_FROM_SOURCE_FOUND})
      set(GMOCK_INCLUDE_DIRS ${GMOCK_FROM_SOURCE_INCLUDE_DIRS})
      set(GMOCK_LIBRARY_DIRS ${GMOCK_FROM_SOURCE_LIBRARY_DIRS})
      set(GMOCK_LIBRARIES ${GMOCK_FROM_SOURCE_LIBRARIES})
      set(GMOCK_MAIN_LIBRARIES ${GMOCK_FROM_SOURCE_MAIN_LIBRARIES})
      set(GMOCK_BOTH_LIBRARIES ${GMOCK_LIBRARIES} ${GMOCK_MAIN_LIBRARIES})
    endif()

    if(GTEST_FROM_SOURCE_FOUND)
      # set the same variables as find_package()
      # do NOT set in the cache since when using gtest from source
      # we must always add the subdirectory to have their targets defined
      set(GTEST_FOUND ${GTEST_FROM_SOURCE_FOUND})
      set(GTEST_INCLUDE_DIRS ${GTEST_FROM_SOURCE_INCLUDE_DIRS})
      set(GTEST_LIBRARY_DIRS ${GTEST_FROM_SOURCE_LIBRARY_DIRS})
      set(GTEST_LIBRARIES ${GTEST_FROM_SOURCE_LIBRARIES})
      set(GTEST_MAIN_LIBRARIES ${GTEST_FROM_SOURCE_MAIN_LIBRARIES})
      set(GTEST_BOTH_LIBRARIES ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES})
    endif()
  else()
    message(STATUS "Found gtest: gtests will be built")
    if(NOT TARGET gtest)
      add_library(gtest SHARED IMPORTED)
      set_target_properties(gtest PROPERTIES IMPORTED_LOCATION "${GTEST_LIBRARIES}")
    endif()
    if(NOT TARGET gtest_main)
      add_library(gtest_main SHARED IMPORTED)
      set_target_properties(gtest_main PROPERTIES IMPORTED_LOCATION "${GTEST_MAIN_LIBRARIES}")
    endif()
    set(GTEST_FOUND ${GTEST_FOUND} CACHE INTERNAL "")
    set(GTEST_INCLUDE_DIRS ${GTEST_INCLUDE_DIRS} CACHE INTERNAL "")
    set(GTEST_LIBRARIES ${GTEST_LIBRARIES} CACHE INTERNAL "")
    set(GTEST_MAIN_LIBRARIES ${GTEST_MAIN_LIBRARIES} CACHE INTERNAL "")
    set(GTEST_BOTH_LIBRARIES ${GTEST_BOTH_LIBRARIES} CACHE INTERNAL "")
  endif()
else()
  message(STATUS "Found gmock: gmock and gtests will be built")
  if(NOT TARGET gmock)
    add_library(gmock UNKNOWN IMPORTED)
    set_target_properties(gmock PROPERTIES IMPORTED_LOCATION "${GMOCK_LIBRARIES}")
  endif()
  if(NOT TARGET gmock_main)
    add_library(gmock_main UNKNOWN IMPORTED)
    set_target_properties(gmock_main PROPERTIES IMPORTED_LOCATION "${GMOCK_MAIN_LIBRARIES}")
  endif()
  set(GMOCK_FOUND ${GMOCK_FOUND} CACHE INTERNAL "")
  set(GMOCK_INCLUDE_DIRS ${GMOCK_INCLUDE_DIRS} CACHE INTERNAL "")
  set(GMOCK_LIBRARIES ${GMOCK_LIBRARIES} CACHE INTERNAL "")
  set(GMOCK_MAIN_LIBRARIES ${GMOCK_MAIN_LIBRARIES} CACHE INTERNAL "")
  set(GMOCK_BOTH_LIBRARIES ${GMOCK_BOTH_LIBRARIES} CACHE INTERNAL "")

  set(GTEST_FOUND ${GMOCK_FOUND} CACHE INTERNAL "")
  set(GTEST_INCLUDE_DIRS ${GMOCK_INCLUDE_DIRS} CACHE INTERNAL "")
  set(GTEST_LIBRARY_DIRS ${GMOCK_LIBRARY_DIRS} CACHE INTERNAL "")
  set(GTEST_LIBRARIES ${GMOCK_LIBRARIES} CACHE INTERNAL "")
  set(GTEST_MAIN_LIBRARIES ${GMOCK_MAIN_LIBRARIES} CACHE INTERNAL "")
  set(GTEST_BOTH_LIBRARIES ${GMOCK_BOTH_LIBRARIES} CACHE INTERNAL "")
endif()
